import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';
// 这里是模糊查询
interface Message {
	filter: {
		pn: number,
		department_no: string,
		post_no: string,
		staff_name: string,
		endtime: string,
		starttime: string,
		is_resident: string,
	    project_no: string
	};
}
export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders) {
	if (message.filter.is_resident === '' && message.filter.project_no === '') {
       // tslint:disable-next-line:no-shadowed-variable
		const query = complex_query(action_id, session_id, systemid);
		// tslint:disable-next-line:no-shadowed-variable
		const dt = await query
		.add_field('department_staff', 'cabinet_staff_no', 'cabinet_staff_no')
		.add_field('department_staff', 'staff_name', 'staff_name')
		.add_field('department_staff', 'staff_no', 'staff_no')
		.add_field('department_staff', 'sex', 'sex')
		.add_field('department_staff', 'post_name', 'post_name')
		.add_field('department_staff', 'staff_state', 'staff_state')
		.add_field('department_staff', 'deptname', 'deptname')
		.add_field('department_staff', 'createdate', 'createdate')
		.add_field('project_personnel', 'is_resident', 'is_resident')
	    .add_field('project_personnel', 'project_name', 'project_name')
		.add_field('project_personnel', 'pub_user_no', 'pub_user_no')
		.inner_join('department_staff','project_personnel',['staff_no','pub_user_no'])
		.where_like('department_staff', 'department_no','%' + message.filter.department_no + '%')
		.where_neq('department_staff', 'createdate', message.filter.starttime)
		.where_lt('department_staff', 'createdate',message.filter.endtime)
		.page(20, message.filter.pn)
		.exec();
		return{dt};
	}
	
	if ( message.filter.is_resident !== '' && message.filter.project_no !== '') {
        const query = complex_query(action_id, session_id, systemid);
		// tslint:disable-next-line:no-shadowed-variable
		const dt = await query
		.add_field('department_staff', 'cabinet_staff_no', 'cabinet_staff_no')
		.add_field('department_staff', 'staff_name', 'staff_name')
		.add_field('department_staff', 'staff_no', 'staff_no')
		.add_field('department_staff', 'sex', 'sex')
		.add_field('department_staff', 'post_name', 'post_name')
		.add_field('department_staff', 'staff_state', 'staff_state')
		.add_field('department_staff', 'deptname', 'deptname')
		.add_field('department_staff', 'createdate', 'createdate')
		.add_field('project_personnel', 'is_resident', 'is_resident')
	    .add_field('project_personnel', 'project_name', 'project_name')
	    // .add_field('project_personnel', 'project_no', 'project_no')
		.add_field('project_personnel', 'pub_user_no', 'pub_user_no')
		.inner_join('department_staff','project_personnel',['staff_no','pub_user_no'])
		.where_neq('department_staff', 'createdate', message.filter.starttime)
		.where_lt('department_staff', 'createdate',message.filter.endtime)
		.page(20, message.filter.pn)
		.exec();
		return{dt};
	}


    if ( message.filter.is_resident ==="" && message.filter.project_no ==="" && message.filter.department_no ==="" && message.filter.staff_name !== ""message.filter.project_no !=="" && message.filter.post_no) {
		const query = complex_query(action_id, session_id, systemid);
		// tslint:disable-next-line:no-shadowed-variable
		const dt = await query
		.add_field('department_staff', 'cabinet_staff_no', 'cabinet_staff_no')
		.add_field('department_staff', 'staff_name', 'staff_name')
		.add_field('department_staff', 'staff_no', 'staff_no')
		.add_field('department_staff', 'sex', 'sex')
		.add_field('department_staff', 'post_name', 'post_name')
		.add_field('department_staff', 'staff_state', 'staff_state')
		.add_field('department_staff', 'deptname', 'deptname')
		.add_field('department_staff', 'createdate', 'createdate')
		.add_field('project_personnel', 'is_resident', 'is_resident')
	    .add_field('project_personnel', 'project_name', 'project_name')
	    // .add_field('project_personnel', 'project_no', 'project_no')
		.add_field('project_personnel', 'pub_user_no', 'pub_user_no')
		.inner_join('department_staff','project_personnel',['staff_no','pub_user_no'])
		.where_like('department_staff', 'staff_name', '%'+message.filter.staff_name + '%')//模糊员工名称
		.page(20, message.filter.pn)
		.exec();
		return{dt};
	}
}
